package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.JwtUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.user.domain.vo.UcenterMemberVo;
import com.ruoyi.user.service.IUcenterMemberService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.vo.EduCommentVo;
import com.ruoyi.edu.domain.bo.EduCommentBo;
import com.ruoyi.edu.service.IEduCommentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论
 *
 * @author ruoyi
 * @date 2022-08-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/edu/comment")
public class EduCommentController extends BaseController {

    private final IEduCommentService iEduCommentService;
    private final IUcenterMemberService iUcenterMemberService;

    /**
     * 查询评论列表
     */
    @SaCheckPermission("edu:comment:list")
    @GetMapping("/list")
    public TableDataInfo<EduCommentVo> list(EduCommentBo bo, PageQuery pageQuery) {
        return iEduCommentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出评论列表
     */
    @SaCheckPermission("edu:comment:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EduCommentBo bo, HttpServletResponse response) {
        List<EduCommentVo> list = iEduCommentService.queryList(bo);
        ExcelUtil.exportExcel(list, "评论", EduCommentVo.class, response);
    }

    /**
     * 获取评论详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("edu:comment:query")
    @GetMapping("/{id}")
    public R<EduCommentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEduCommentService.queryById(id));
    }

//    @GetMapping("getCommentPage")
//    public TableDataInfo<EduCommentVo> getCommentPage(PageQuery pageQuery,
//                            String courseId){
//       return iEduCommentService.getCommentPageByid(pageQuery,courseId);
//    }
    /**
     * 新增评论
     */
    @SaCheckPermission("edu:comment:add")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(HttpServletRequest request, @Validated(AddGroup.class) @RequestBody EduCommentBo bo) throws Exception {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
//        判断用户是否登录
        String message  = "请先登录";
        if(StringUtils.isEmpty(memberIdByJwtToken)){
            throw new Exception(message);
        }
        bo.setMemberId(memberIdByJwtToken);

        UcenterMemberVo ucenterMemberVo = iUcenterMemberService.queryById(memberIdByJwtToken);
        bo.setAvatar(ucenterMemberVo.getAvatar());
        bo.setNickname(ucenterMemberVo.getNickname());
        return toAjax(iEduCommentService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改评论
     */
    @SaCheckPermission("edu:comment:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EduCommentBo bo) {
        return toAjax(iEduCommentService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除评论
     *
     * @param ids 主键串
     */
    @SaCheckPermission("edu:comment:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iEduCommentService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
